package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Result<?> userLogin(User user) {
        QueryWrapper<User> queryWrapper =  new QueryWrapper<>();
        User res = userMapper.selectOne(queryWrapper.lambda().eq(User::getPhoneNumber,user.getPhoneNumber()).eq(User::getPassword,user.getPassword()));
        if (res == null){
            return Result.error("-1","用户名或密码有误！");
        }
        return Result.success(res);
    }

    @Override
    public Result<?> userRegister(User user) {
        QueryWrapper<User> queryWrapper =  new QueryWrapper<>();
        User res = userMapper.selectOne(queryWrapper.lambda().eq(User::getPhoneNumber,user.getPhoneNumber()));
        if (res != null){
            return Result.error("-1","该用户已存在！");
        }
        user.setDeleted(0);
        userMapper.insert(user);
        return Result.success();
    }

    @Override
    public IPage<User> selectPage(Integer pageNum, Integer pageSize, String nickName, String username, String sex, String phoneNumber){
        QueryWrapper<User> queryWrapper =  new QueryWrapper<>();
        Page<User> page = new Page<>(pageNum,pageSize);
        if (StrUtil.isNotBlank(nickName)){
            queryWrapper.lambda().like(User::getNickName,nickName);
        }
        if (StrUtil.isNotBlank(username)){
            queryWrapper.lambda().like(User::getUsername,username);
        }
        if (StrUtil.isNotBlank(sex)){
            queryWrapper.lambda().eq(User::getSex,sex);
        }
        if (StrUtil.isNotBlank(phoneNumber)){
            queryWrapper.lambda().eq(User::getPhoneNumber,phoneNumber);
        }
        return userMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Result<?> addUser(User user) {
       user.setDeleted(0);
       userMapper.insert(user);
       return Result.success();
    }

    @Override
    public Result<?> updateUser(User user) {
        userMapper.updateById(user);
        return Result.success();
    }

    @Override
    public Result<?> deleteUser(int userId) {
        userMapper.deleteById(userId);
        return Result.success();
    }

    @Override
    public User selectById(int userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public IPage<User> selectUserRole(Integer pageNum, Integer pageSize,String roleId,String phoneNumber,String username) {
//        IPage<User> userList = userMapper.selectUserRole();
//        return userList;
        return null;
    }

}
